
/**
 * 防抖:一定时间范围内连续点击时，只执行最后一次
 * */
export const debounce = (fn, timer = 1000) => {
	let isClick = null;
	return function() {
		const _this = this;
		if (isClick) {
			clearTimeout(isClick);
		}
		const arg = arguments;
		isClick = setTimeout(() => {
			// 返回参数和指向
			fn.call(_this, arg, _this);
		}, timer);
	};
};

/**
 * 节流: 一定时间范围内连续点击时，只执行第一次点击
 * */
export const throttle = (fn, timer = 1000) => {
	let isClick = false;
	return function() {
		const _this = this;
		const arg = arguments;

		if (!isClick) {
			isClick = true;
			// 返回参数和指向
			fn.call(_this, arg, _this);
			setTimeout(() => {
				isClick = false;
			}, timer);
		}
	};
};
export const formatHours = (date) =>{
	let d = new Date(date);
	let hour = d.getHours() < 10 ? `0${d.getHours()}` : d.getHours();
	let minute = d.getMinutes() < 10 ? `0${d.getMinutes()}` : d.getMinutes();
	let second = d.getSeconds() < 10 ? `0${d.getSeconds()}` : d.getSeconds();
	return ` ${hour}:${minute}:${second}`;
};
export const formatDate = (e) =>{
	let date = e;
	if (typeof e === 'string') {
		date = e.replace(/-/g, '/')
	}
	let d = new Date(date);
	let year = d.getFullYear();
	let month = (d.getMonth() + 1) < 10 ? `0${d.getMonth() + 1}` : d.getMonth() + 1;
	let day = d.getDate() < 10 ? `0${d.getDate()}` : d.getDate();
	return `${year}-${month}-${day}`;
};
function filZero(e) {
	return e >= 10 ? e : `0${e}`;
}
// 获取时分秒
export const formatHMS = (e) => {
	let dStr = date;
	if (typeof date === 'string') {
		dStr = dStr.replace(/-/g, '/');
	}
	const d = new Date(dStr);
	const hour = d.getHours();
	const minute = d.getMinutes();
	const second = d.getSeconds();
	return `${filZero(hour)}:${filZero(minute)}:${filZero(second)}`;
};
export const getHMS = (e = Date.now()) => {
	let dStr = date;
	if (typeof date === 'string') {
		dStr = dStr.replace(/-/g, '/');
	}
	const d = new Date(dStr);
	const hour = d.getHours();
	const minute = d.getMinutes();
	const second = d.getSeconds();
	return `${filZero(hour)}:${filZero(minute)}:${filZero(second)}`;
};
export const formatYearMonth = (date) =>{
	let dStr = date;
	if (typeof date === 'string') {
		dStr = dStr.replace(/-/g, '/');
	}
	let d = new Date(dStr);
	let year = d.getFullYear();
	let month = (d.getMonth() + 1) < 10 ? `0${d.getMonth() + 1}` : d.getMonth() + 1;
	return `${year}-${month}`;
};
export const formatMonthDate = (date, f = '-') =>{
	let dStr = date;
	if (typeof date === 'string') {
		dStr = dStr.replace(/-/g, '/');
	}
	let d = new Date(dStr);
	let month = (d.getMonth() + 1) < 10 ? `0${d.getMonth() + 1}` : d.getMonth() + 1;
	let day = d.getDate() < 10 ? `0${d.getDate()}` : d.getDate();
	return `${month}${f}${day}`;
};
export const formatTime = (e) =>{
	let date = e;
	if (typeof date === 'string') {
		date = date.replace(/-/g, '/')
	}
	let d = new Date(date);
	let year = d.getFullYear();
	let month = (d.getMonth() + 1) < 10 ? `0${d.getMonth() + 1}` : d.getMonth() + 1;
	let day = d.getDate() < 10 ? `0${d.getDate()}` : d.getDate();
	let hour = d.getHours() < 10 ? `0${d.getHours()}` : d.getHours();
	let minute = d.getMinutes() < 10 ? `0${d.getMinutes()}` : d.getMinutes();
	let second = d.getSeconds() < 10 ? `0${d.getSeconds()}` : d.getSeconds();
	return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
};

// 获取日期
export const getCurrentDate = () =>{
    let d = new Date();
    let year = d.getFullYear();
    let month = (d.getMonth() + 1) < 10 ? `0${d.getMonth() + 1}` : d.getMonth() + 1;
    let day = d.getDate() < 10 ? `0${d.getDate()}` : d.getDate();
    return `${year}-${month}-${day}`;
};
// 获取年月
export const getCurrentMonth = () =>{
	let d = new Date();
	let year = d.getFullYear();
	let month = (d.getMonth() + 1) < 10 ? `0${d.getMonth() + 1}` : d.getMonth() + 1;
	return `${year}-${month}`;
};

// 获取时间
export const getCurrentDateTime = () =>{
    let d = new Date();
    let year = d.getFullYear();
    let month = (d.getMonth() + 1) < 10 ? `0${d.getMonth() + 1}` : d.getMonth() + 1;
    let day = d.getDate() < 10 ? `0${d.getDate()}` : d.getDate();
    let hour = d.getHours() < 10 ? `0${d.getHours()}` : d.getHours();
    let minute = d.getMinutes() < 10 ? `0${d.getMinutes()}` : d.getMinutes();
    let second = d.getSeconds() < 10 ? `0${d.getSeconds()}` : d.getSeconds();
    return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
};
export const showLoading = (type = true, title = '加载中') => {
	if (type) {
		wx.showLoading({
			title: title
		});
	} else {
		wx.hideLoading();
	}
};

export const tipBar = (type = 'validator', title = '', icon) => {
	if (type === 'save') {
		wx.showToast({
			title: title || '操作成功！',
			icon: 'success'
		});
	} else if (type === 'validator') {
		wx.showToast({
			title: title || '请填写完整！',
			icon: 'error'
		});
	} else if (type === 'none') {
		wx.showToast({
			title: '请选择操作对象',
			icon: 'error'
		});
	} else if (type === 'custom') {
		wx.showToast({
			title: title,
			icon
		});
	}
};

export const pageConfig = {
	pageSize: 30,
	totalCount: 50,
};

/**
 * 根据起、止时间算截止天数，精确到时分秒
 * 注：1、传时间格式时
 * 返回：788天17时4分46秒
 * */
export const getDayTimeDiff = function(startTime, endTime) {
	const d1 = new Date(startTime).getTime();
	const d2 = new Date(endTime).getTime();
	const diff = (d2 - d1) / 1000;
	let day = Math.floor(diff / (24 * 60 * 60));
	// 跨天加1
	if (formatDate(endTime) > formatDate(startTime)) {
		day = day + 1;
	}
	const house = Math.floor((diff / (60 * 60)) % 24);
	const minute = Math.floor((diff / 60) % 60);
	const second = Math.floor(diff % 60);
	return `${day}天${house}时${minute}分${second}秒`;
};

/**
 * 根据起、止日期算天数
 * 注：1、传时间格式时
 * 返回：9.5天
 * */
export const getDayDiff = function(startTime, endTime) {
	const d1 = new Date(startTime).getTime();
	const d2 = new Date(endTime).getTime();
	const diff = (d2 - d1) / 1000;
	let day = Math.floor(diff / (24 * 60 * 60));
	// 跨天加1
	if (formatDate(endTime) > formatDate(startTime)) {
		day = day + 1;
	}
	const house = Math.floor((diff / (60 * 60)) % 24) / 24;
	const d = (day + house).toFixed(1);
	return Number(d);
};

// 获取相差小时数
export const getDiffHours = function(startTime, endTime) {
	let hours = 0;
	// 结束时间必须大于开始时间
	if (endTime > startTime) {
		const startSta = new Date(startTime);
		const endSta = new Date(endTime);

		const diffSecond = endSta - startSta;
		hours = Number((diffSecond / 60 / 60 / 1000).toFixed(2));
	} else {
		hours = -1;
	}
	return hours;
};
export const isLastPage = (total, pageIndex, pageSize = pageConfig.pageSize) => {
	let isEnd = false;
	if (pageIndex >= Math.ceil(total / pageSize)) {
		isEnd = true;
	} else {
		isEnd = false;
	}
	return isEnd;
};
